package com.ceres.farm.service.impl;

import com.ceres.farm.dao.CustFarmMapper;
import com.ceres.farm.service.AreaService;
import com.ceres.farm.pojo.CustFarm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    CustFarmMapper custFarmMapper;
    /**
     * 添加农场片区
     * @param custFarm
     */
    @Override
    public void areaAdd(CustFarm custFarm) {
        int insert = custFarmMapper.add(custFarm);
        if (insert==0){
            throw new RuntimeException("添加失败");
        }

    }

    /**
     * 修改农场片区
     * @param custFarm
     */
    @Override
    public void areaUpdate(CustFarm custFarm) {
        int i = custFarmMapper.updateByPrimaryKey(custFarm);
        if (i==0){
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 根据条件进行分页查询
     * @param pageNumber
     * @param pageSize
     * @param custid
     * @param name
     * @return
     */
    @Override
    public Page<CustFarm> findPage(Integer pageNumber, Integer pageSize, Integer custid, String name) {

        PageHelper.startPage(pageNumber, pageSize);
        Example example=new Example(CustFarm.class);

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("custid",custid);
        criteria.andEqualTo("name",name);
        Page<CustFarm> page=(Page<CustFarm>)custFarmMapper.selectByExample(example);
    return page;
    }

    /**
     * 根据ID进行查询
     * @param id
     * @return
     */
    @Override
    public CustFarm findById(Integer id) {
        CustFarm custFarm = custFarmMapper.selectByPrimaryKey(id);
        return custFarm;
    }
}
